﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;

namespace TextEditor
{
    public class ZYTextNumInput : ZYTextInput
    {
        public override string GetXMLName()
        {
            return "numinput";
        }

        public bool checkComplete = true;

        public float MinNumber
        {
            get
            {
                return this.Attributes.GetFloat("min-number");
            }
            set
            {
                this.Attributes.SetValue("min-number", value);
            }
        }

        public override void DrawBackGround(ZYTextElement myElement)
        {
            if (checkComplete)
            {
                Rectangle b2 = new Rectangle(myElement.RealLeft, myElement.RealTop, myElement.Width, myElement.Height - 1);
                base.myOwnerDocument.View.FillRectangle(Color.FromArgb(175, Color.Red), b2);
            }
            base.DrawBackGround(myElement);
        }

        public override bool HandleMouseMove(int x, int y, System.Windows.Forms.MouseButtons Button)
        {
            if (checkComplete && this.Contains(x, y))
            {
                base.OwnerDocument.OwnerControl.setToolTip("最小值：" + this.MinNumber.ToString() + "\n最大值：" + this.MaxNumber.ToString(), this.Bounds, this);
                return true;
            }
            return base.HandleMouseMove(x, y, Button);
        }

        public override bool RefreshView()
        {
            string numstr = this.Text;
            Regex r = new Regex(@"^[-]?\d+[.]?\d*$");
            if (!r.IsMatch(numstr))
            {
                checkComplete = true;
            }
            else
            {
                float f = CheckIsFloat(numstr);
                if (f >= this.MinNumber && f <= this.MaxNumber)
                {
                    checkComplete = false;
                }
                else
                {
                    checkComplete = true;
                }
            }
            return base.RefreshView();
        }

        public float MaxNumber
        {
            get
            {
                return this.Attributes.GetFloat("max-number");
            }
            set
            {
                this.Attributes.SetValue("max-number", value);
            }
        }

        public float SmallLength
        {
            get
            {
                return this.Attributes.GetFloat("max-number");
            }
            set
            {
                this.Attributes.SetValue("max-number", value);
            }
        }
        public static float CheckIsFloat(string inputValue)
        {
            float f;
            float.TryParse(inputValue, out f);
            return f;
        }
    }
}
